/*
* Xg.js 1.0 Copyright (c) 2016-2017 
* 常用组件
*/

/* ========================================================================
    验证组件
  ========================================================================*/
(function(){
  function Validate(element,options) {
      this.element=$(element);
      this.options = $.extend({}, Validate.defaults, options);
      this.form=$(this.options.form);
      if(!this.form.length){
          this.form =this.element.closest("form");
      }
  }
  Validate.defaults={
     rtips:'不能为空',
     rmobile:'手机号码格式不正确',
     remail:'邮箱格式不正确',
     rpassword:'密码不一致',
     success:function(){}
  }
  Validate.prototype.Varinit=function(){//变量初始
     this.ispass        = false;//非ajax提交验证判断
     this.regularmobile = /(^((\+?86)|(\(\+86\)))?\d{3,4}-{1}\d{7,8}(-{0,1}\d{3,4})?$)|(^((\+?86)|(\(\+86\)))?1\d{10}$)/;
     this.regularEmail  = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
     this.tipsTemplate  = "<p class='require_tips'><span class='tips_icon'></span>#tips#</p>";

     this.inputlist     = this.form.find("input");
     this.require       = this.form.find(".require");
     this.writeyz       = this.form.find(".writeyz");
     this.password      = this.form.find(".password");
     this.rpassword     = this.form.find(".rpassword");
  }
  Validate.prototype.Verify=function(){//验证核心
     this.Varinit();
     var that=this;

    /*===点击验证===*/
    this.element.click(function(){
        that.i=0;
        that.nlength=0;
        that.require.each(function(){//必须填写验证
          $(this).next(".require_tips").remove();
          that.MustProof(this);
        });
        that.writeyz.each(function(){//非必须填写，格式验证
          $(this).next(".require_tips").remove();
            that.NoMustProof(this);
        });
        if(that.i==that.require.length+that.nlength){//全部验证完毕条件
          that.ispass=true;
          that.options.success(that.element);
        }
        return that.ispass;
    })

    /*===单个失焦验证===*/
    this.require.blur(function(){
       that.MustProof(this);
    })
    this.writeyz.blur(function(){
       that.NoMustProof(this);
    })
   /*===验证前去除提示===*/
   this.require.focus(function(){
      $(this).next().remove();
    })
    this.writeyz.focus(function(){
      $(this).next().remove();
    })
  }
  Validate.prototype.MustProof=function(obj){//必须填写验证方法
      var wval = $(obj).val();
      var reqtips=$(obj).data('reqtips');
      var dval = obj.defaultValue;
      typeof reqtips ==='undefined' ? reqtips =this.options.rtips : reqtips =reqtips;
      var rtips= this.tipsTemplate.replace('#tips#',reqtips);
      if($(obj).prop('type')=='checkbox'){
          if(!$(obj).prop('checked'))
             $(obj).after(rtips);
          else
              this.Rule(obj);
      }
     if($(obj).prop('type')=='select-one'){
         if ($(obj).data('option')==$(obj).find('option:selected').text()){
             $(obj).after(rtips);
         }else{
            this.Rule(obj);
         }
      }
      else if(wval=='' || wval==dval)//判断与默认的val值
         $(obj).after(rtips);
      else
          this.Rule(obj);

  }
   Validate.prototype.NoMustProof=function(obj){//非必须填写验证方法
      var wval=$(obj).val();
      var dval=obj.defaultValue;

      if(wval!='' && wval!=dval){
          this.nlength++;
          this.Rule(obj);
      }
  }
  Validate.prototype.Rule=function(obj){//格式验证
    var className=$(obj).attr('class');
      if(className.indexOf('rmobile')>=0)
          (this.regularmobile.test($(obj).val())==true) ? this.i++ : $(obj).after(this.tipsTemplate.replace('#tips#',this.options.rmobile));
      else if(className.indexOf('remail')>=0)
          (this.regularEmail.test($(obj).val())==true) ? this.i++  : $(obj).after(this.tipsTemplate.replace('#tips#',this.options.remail));
      else if(className.indexOf('rpassword')>=0)//为注册页增加个确认密码
          (this.password.val()==this.rpassword.val()) ? this.i++   : $(obj).after(this.tipsTemplate.replace('#tips#',this.options.rpassword));
      else
          this.i++;
  }
  /*===插件定义===*/
  function Plugin(options) {//插件定义缓存
      return this.each(function () {
          var $this=$(this);
          var data=$this.data('validate');
          var option= $.extend({}, Validate.defaults,typeof options == 'object' && options,$this.data());
          if(!data){
              $this.data('validate', (data=new Validate($this, option)))
          }
          data.Verify();
      })
  }
  
  /*调用*/
  $.fn.Validate= Plugin;

})(window.jQuery || window.Zepto);

/* ========================================================================
    省市区街道多级联动
  ========================================================================*/
(function(){
    function Address(element) {
      this.element=$(element);
  }
  Address.prototype.Varinit=function(){//变量初始
    this.province = this.element.find('.province');
    this.city     = this.element.find('.city');
    this.area = this.element.find('.area');
    this.district = this.element.find('.district');
    this.defaultProvince=this.province.data('value');
    this.defaultCity=this.city.data('value');
    this.defaultArea=this.area.data('value');
  }
  Address.prototype.SiteInit=function(){
    this.Varinit();
     /*显示默认地址*/
    this.GetList(this.province,0,this.defaultProvince);//省份
    this.GetList(this.city,this.defaultProvince,this.defaultCity);
    this.GetList(this.area,this.defaultCity,this.defaultArea);

    var that=this,val;
    this.province.change(function(){//城市
       val=$(this).val();
       that.getText(this);
       that.area.html("<option value=''>"+that.area.find('option').first().text()+"</option>");
       that.district.html("<option value=''>"+that.district.find('option').first().text()+"</option>");
       that.GetList(that.city,val)
    })
    this.city.change(function(){//地区
       val=$(this).val();
       that.getText(this);
       that.district.html("<option value=''>"+that.district.find('option').first().text()+"</option>");
       that.GetList(that.area,val)
    })
    this.area.change(function(){//街道
       val=$(this).val();
       that.getText(this);
       that.GetList(that.district,val)
    })
  }
    Address.prototype.getText=function(obj){
    var atext=$(obj).find('option:selected').text();
    var $parent=$(obj).parent();
     $parent.find('.AreaText').text(atext);
     $parent.find("input[type='hidden']").val(atext);
  }
  Address.prototype.GetList=function(obj,id,def){
     var str="";
     var firstOption=obj.find('option').first().text();
     obj.html("<option value=''>"+firstOption+"</option>");
     $.ajaxUrl('get','http://auth.pianor.com/district_get.html',{parent_code:id},'jsonp',true,function(response){
          $.each(response,function(k,v){
              if(v.code==def){
                  str+="<option value="+v.code+" selected>"+v.name+"</option>"
              }else{
                  str+="<option value="+v.code+">"+v.name+"</option>"
              }
              
          });
          obj.append(str)
    });
  }
  /*===插件定义===*/
  function Plugin(options) {//插件定义缓存
      return this.each(function () {
          var $this=$(this);
          var data=$this.data('Address');
          if(!data){
              $this.data('Address', (data=new Address($this)))
          }
          data.SiteInit();
      })
  }
  
  /*调用*/
  $.fn.Address= Plugin;
})(window.jQuery || window.Zepto);

